import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageProcessor;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class scores{

	int pixelcounter =0;
	double TotalPixelWeight =0 , PixelWeight = 0;


	public void band_score(ImagePlus imp, ImagePlus mask_Channel1, ImagePlus mask_Channel2){

		ImageProcessor mask_ip_1 = mask_Channel1.getProcessor();
		ImageProcessor mask_ip_2 = mask_Channel2.getProcessor();

		DistancePoint dp = new DistancePoint();	
		int  c,r,g,b;
		int dcounter = 0;
		double Dist,fall_of_const = Analysis_.fall_c, minDist = 1000000000;
        int all_edge_pixel =0;

		edge selected_edge;

		ImageProcessor ip = imp.getProcessor();
        mesh me = new mesh();

		for(int y =0; y < imp.getHeight(); y++){
			for (int x =0; x< imp.getWidth(); x++){

				
				
		  if ((mask_ip_1.getPixel(x, y) == 255)  &&  (mask_ip_2.getPixel(x, y) == 255)){  
				
			    c = ip.getPixel(x,y);
				r = (c&0xff0000)>>16;
				g = (c&0xff00)>>8;
				b = c&0xff;

			  
			double    x_c = me.change_coordinate_X(x); 
			double    y_c = me.change_coordinate_Y(y);

				//float fx = x - center_mass.xMass;
				//float fy = y - center_mass.yMass;
				
				float fx = (float)(x_c - me.change_coordinate_X(center_mass.xMass));//x dist from center of mass of cell
				float fy = (float)(y_c - me.change_coordinate_Y(center_mass.yMass));
				
				double hyp = Math.sqrt(fx*fx + fy*fy);  
				double length_edge =0;
				//	selectedEdge_x =0; selectedEdge_y =0; selectedEdge_x2 =0; selectedEdge_y2 =0;
				minDist = 100000000;
				
				selected_edge = new edge();
				double length_edge_point2 =0;
				double length_edge_point1 =0;
				int bestEdge =0;
			// if point is yellow 
			//if (r == 255 && g == 255 && mask_ip_myosin.getPixel(x, y) == 255 && mask_ip.getPixel(x, y) == 255){
			//if (r == 255 && g == 255 && (mask_ip_1.getPixel(x, y) == 255)  &&  (mask_ip_1.getPixel(x, y) == 255)){
			      
				if(hyp>7){ //make this magic constant determined by mesh size!!!!!!
					for (int m=0; m<band.numVerts; m++){

						// Boolean end_point_flag = false;

						Dist =  dp.distanceToSegment(x_c, y_c, me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1),  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

						if(Dist <= minDist && Dist >= 0 ){
							minDist =  Dist;
							if(dp.end_point_flag != 0)
							{
								//System.out.println("Inside end point "+m);
								// Distace bet x,y and  x1 , y1 of m  and x2, y2 of m +1
								if ( (m!=0) && (m!= (band.numVerts-1)))
								{

									if(dp.end_point_flag == 2)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m+1].x2) ,  me.change_coordinate_Y(band.ed[m+1].y2)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1));	                        						

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m+1;
									}
									else if(dp.end_point_flag == 1)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m-1].x1) ,  me.change_coordinate_Y(band.ed[m-1].y1)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m-1;

									}

								}

								if( m == 0)
								{
									if(dp.end_point_flag == 2)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m+1].x2) ,  me.change_coordinate_Y(band.ed[m+1].y2)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1));	                        						

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m+1;
									}
									else if(dp.end_point_flag == 1)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[band.numVerts -1].x1) ,  me.change_coordinate_Y(band.ed[band.numVerts -1].y1)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = band.numVerts -1;

									}

								}


								if( m == (band.numVerts -1))
								{
									if(dp.end_point_flag == 2)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[0].x2) ,  me.change_coordinate_Y(band.ed[0].y2)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1));	                        						

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = 0;
									}
									else if(dp.end_point_flag == 1)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m -1].x1) ,  me.change_coordinate_Y(band.ed[m -1].y1)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m -1;

									}

								}

							} 
							else
							{
								bestEdge = m;
							}



							selected_edge =  band.ed[m];
							selected_edge.edge_number = m;
						}


					}

					selected_edge.pixel_counter++;
				
					//if( selected_edge.edge_number  == 54)
					//{
					//	ip_rgb.putPixel(x, y, 343434343);
					//}

					length_edge = Math.sqrt((me.change_coordinate_X(selected_edge.x1) -  me.change_coordinate_X(selected_edge.x2))*(me.change_coordinate_X(selected_edge.x1) - me.change_coordinate_X(selected_edge.x2)) + (me.change_coordinate_Y(selected_edge.y1) - me.change_coordinate_Y(selected_edge.y2))* (me.change_coordinate_Y(selected_edge.y1) - me.change_coordinate_Y(selected_edge.y2)));
					//System.out.println("Edge: "+selected_edge.x1+" "+selected_edge.y1+" "+selected_edge.x2+" "+selected_edge.y2  +" :"+minDist+"  ");

		//			try {
		//				bufferedWriter.write("m: "+bestEdge +"Point"+x+" "+y+" Edge: "+selected_edge.x1+" "+selected_edge.y1+" "+selected_edge.x2+" "+selected_edge.y2  +" :"+minDist+"  ");
		//			} catch (IOException e) {
						// TODO Auto-generated catch block
	//					e.printStackTrace();
		//			}

					pixelcounter++;
					if(r ==255 && g ==255){
						PixelWeight = (double)(1/Math.pow((minDist + 1), fall_of_const));
						selected_edge.edge_score += (PixelWeight/length_edge);
						// selected_edge.edge_score += (PixelWeight);
					}
					else
					{
						PixelWeight = (-1)*(double) (1/Math.pow((minDist + 1), fall_of_const));
						selected_edge.edge_score += (PixelWeight/length_edge);
						//selected_edge.edge_score += (PixelWeight);
						dcounter++;
					}
					TotalPixelWeight = TotalPixelWeight + PixelWeight;
					
				} // if hyp > 100
			}   // end of check if point is within mask

			} //next pixel

		

		//	try {
		//		bufferedWriter.write("\n");
		//	} catch (IOException e) {
				// TODO Auto-generated catch block
		//		e.printStackTrace();
		//	}

		}// next row
		all_edge_pixel = 0;
		for(int pi_c=0; pi_c <band.numVerts; pi_c++){
			
			all_edge_pixel = all_edge_pixel+  band.ed[pi_c].pixel_counter;
		}
			
		System.out.println("edge pixels"+all_edge_pixel);
		
		System.out.println(pixelcounter+"Dcounter:"+dcounter);
		IJ.showMessage("Band score :"+TotalPixelWeight);


	}

	public void band_score_user_defined(){

		ImageProcessor mask_ip_1 = Analysis_.mask_Channel1.getProcessor();
		ImageProcessor mask_ip_2 = Analysis_.mask_Channel2.getProcessor();

		DistancePoint dp = new DistancePoint();	
		int  c,r,g,b;

		TotalPixelWeight =0; PixelWeight = 0;
		pixelcounter =0;

		double Dist, TotalDist,fall_of_const = Analysis_.fall_c, edge_scores[]= new double[Analysis_.numVertex_User_Defined], minDist = 1000000000;


		edge selected_edge;

		ImageProcessor ip = Analysis_.imp_rgb_modified.getProcessor();
		mesh me = new mesh();

		BufferedWriter bufferedWriter = null;
	//	try {
	//		bufferedWriter = new BufferedWriter(new FileWriter("edgepoint.txt"));
	//	} catch (IOException e) {
	//		// TODO Auto-generated catch block
	//		e.printStackTrace();
	//	}

		for(int y =0; y < Analysis_.imp_rgb_modified.getHeight(); y++){
			for (int x =0; x< Analysis_.imp_rgb_modified.getWidth(); x++){

		if ((mask_ip_1.getPixel(x, y) == 255)  &&  (mask_ip_1.getPixel(x, y) == 255)){
				
				c = ip.getPixel(x,y);
				r = (c&0xff0000)>>16;
				g = (c&0xff00)>>8;
				b = c&0xff;

				double    x_c = me.change_coordinate_X(x);
				double    y_c = me.change_coordinate_Y(y);

				
			    float fx = (float)(x_c - me.change_coordinate_X(center_mass.xMass));
				float fy = (float)(y_c - me.change_coordinate_Y(center_mass.yMass));
				
				double hyp = Math.sqrt(fx*fx + fy*fy);  
				double length_edge =0;
				//	selectedEdge_x =0; selectedEdge_y =0; selectedEdge_x2 =0; selectedEdge_y2 =0;
				minDist = 100000000;

				selected_edge = new edge();
				double length_edge_point2 =0;
				double length_edge_point1 =0;
				int bestEdge =0;
			// if point is yellow 
			//if (r == 255 && g == 255 && mask_ip_myosin.getPixel(x, y) == 255 && mask_ip.getPixel(x, y) == 255){
			//if (r == 255 && g == 255 && (mask_ip_1.getPixel(x, y) == 255)  &&  (mask_ip_1.getPixel(x, y) == 255)){
				if(hyp>15){
					for (int m=0; m<Analysis_.numVertex_User_Defined; m++){

						// Boolean end_point_flag = false;

						Dist =  dp.distanceToSegment(x_c, y_c, me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1),  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));
						if(Dist <= minDist && Dist >= 0 ){
							minDist =  Dist;
							if(dp.end_point_flag != 0)
							{
								//System.out.println("Inside end point "+m);
								// Distace bet x,y and  x1 , y1 of m  and x2, y2 of m +1
								if ( (m!=0) && (m!= (Analysis_.numVertex_User_Defined-1)))
								{

									if(dp.end_point_flag == 2)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m+1].x2) ,  me.change_coordinate_Y(band.ed[m+1].y2)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1));	                        						

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m+1;
									}
									else if(dp.end_point_flag == 1)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m-1].x1) ,  me.change_coordinate_Y(band.ed[m-1].y1)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m-1;

									}

								}

								if( m == 0)
								{
									if(dp.end_point_flag == 2)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m+1].x2) ,  me.change_coordinate_Y(band.ed[m+1].y2)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1));	                        						

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m+1;
									}
									else if(dp.end_point_flag == 1)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[Analysis_.numVertex_User_Defined -1].x1) ,  me.change_coordinate_Y(band.ed[Analysis_.numVertex_User_Defined -1].y1)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = Analysis_.numVertex_User_Defined -1;

									}

								}


								if( m == (Analysis_.numVertex_User_Defined -1))
								{
									if(dp.end_point_flag == 2)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[0].x2) ,  me.change_coordinate_Y(band.ed[0].y2)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x1),  me.change_coordinate_Y(band.ed[m].y1));	                        						

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = 0;
									}
									else if(dp.end_point_flag == 1)
									{

										length_edge_point1 = dp.distance(x_c, y_c, me.change_coordinate_X(band.ed[m -1].x1) ,  me.change_coordinate_Y(band.ed[m -1].y1)); 
										length_edge_point2 =  dp.distance(x_c, y_c,  me.change_coordinate_X(band.ed[m].x2),  me.change_coordinate_Y(band.ed[m].y2));

										if ( length_edge_point1 > length_edge_point2)
										{
											bestEdge = m;
										}
										else
											bestEdge = m -1;

									}

								}

							} 
							else
							{
								bestEdge = m;
							}



							selected_edge =  band.ed[m];
							selected_edge.edge_number = m;
						}


					}

					//if( selected_edge.edge_number  == 54)
					//{
					//	ip_rgb.putPixel(x, y, 343434343);
					//}

					length_edge = Math.sqrt((me.change_coordinate_X(selected_edge.x1) - me.change_coordinate_X(selected_edge.x2))*(me.change_coordinate_X(selected_edge.x1) - me.change_coordinate_X(selected_edge.x2)) + (me.change_coordinate_Y(selected_edge.y1) - me.change_coordinate_Y(selected_edge.y2))* (me.change_coordinate_Y(selected_edge.y1) - me.change_coordinate_Y(selected_edge.y2)));
					//System.out.println("Edge: "+selected_edge.x1+" "+selected_edge.y1+" "+selected_edge.x2+" "+selected_edge.y2  +" :"+minDist+"  ");

				//	try {
				//		bufferedWriter.write("m: "+bestEdge +"Point"+x+" "+y+" Edge: "+selected_edge.x1+" "+selected_edge.y1+" "+selected_edge.x2+" "+selected_edge.y2  +" :"+minDist+"  ");
				//	} catch (IOException e) {
				//		// TODO Auto-generated catch block
				//		e.printStackTrace();
				//	}

					pixelcounter++;
					if(r ==255 && g ==255){
						PixelWeight = (double)(1/Math.pow((minDist + 1), fall_of_const));
						selected_edge.edge_score += (PixelWeight/length_edge);
						// selected_edge.edge_score += (PixelWeight);
					}
					else
					{
						PixelWeight = (-1)*(double) (1/Math.pow((minDist + 1), fall_of_const));
						selected_edge.edge_score += (PixelWeight/length_edge);
						//selected_edge.edge_score += (PixelWeight);
					}
					TotalPixelWeight = TotalPixelWeight + PixelWeight;

				} // if hyp > 100
			}   // end of check if point is within mask



			} //next pixel

		}// end row


		IJ.showMessage("Band score :"+TotalPixelWeight);

	}




}